package trong.gold.soft.com.view;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.font.VietnameseFont;
import trong.gold.soft.com.main.mTetrisMidlet;
import trong.gold.soft.com.view.game.startGame;
public class menuSlGame extends GameCanvas{
	private Image[] clickmenuImg,unclickmenuImg;
	private String[] clickmenuName = {"classic.png","hotgirl.png","hiendai.png"};
	private String[] unclickmenuName = {"unclassic.png","unhotgirl.png","unhiendai.png"};
	private String[] name = {"Cổ điển","Hot girl","Hiện đại"};
	private static int curr=0;
	private int latx=0,laty=0;
	private Graphics g;
	private mTetrisMidlet midlet;
	private menuSlGame menugame;
	public menuSlGame(mTetrisMidlet midlet) {
		super(false);
		this.midlet = midlet;
		this.g = this.getGraphics();
		setFullScreenMode(true);
		clickmenuImg = new Image[clickmenuName.length];
		unclickmenuImg = new Image[clickmenuName.length];
		for(int i=0;i<clickmenuName.length;i++){
			clickmenuImg[i] = createImg.createImage("/com/image/iconmenu/menugame/" + clickmenuName[i]);
			unclickmenuImg[i] = createImg.createImage("/com/image/iconmenu/menugame/" + unclickmenuName[i]);
		}
		Draw();
	}
	private void Draw(){
		for(int i=0;i<=this.getWidth()/44;i++){
			g.drawImage(Resources.backgr, i*Resources.backgr.getWidth(), 0, Resources.TOP_LEFT_ANCHOR);
		}
		g.drawImage(Resources.imgicon, this.getWidth()/2 - Resources.imgicon.getWidth()/2, 10, Resources.TOP_LEFT_ANCHOR);
		g.setColor(0xb8f1e2);
		g.fillRect(0, this.getHeight()/2 - 48, this.getWidth(), 96);
		VietnameseFont.BlackBold.DrawString(g, name[0],this.getWidth()/2, this.getHeight()/2 - clickmenuImg[0].getHeight()/2 - 20, Graphics.HCENTER);
		g.drawImage(clickmenuImg[0], this.getWidth()/2 - clickmenuImg[0].getWidth()/2, this.getHeight()/2 - clickmenuImg[0].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
		g.drawImage(unclickmenuImg[clickmenuName.length-1], this.getWidth()/2 - this.getWidth()/4 - unclickmenuImg[0].getWidth()/2,this.getHeight()/2 - clickmenuImg[0].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
		g.drawImage(unclickmenuImg[1], this.getWidth()/2 + this.getWidth()/4 - unclickmenuImg[0].getWidth()/2,this.getHeight()/2 - clickmenuImg[0].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, "Chọn",this.getWidth()/2, this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		VietnameseFont.BlackBold.DrawString(g, "Trở về",this.getWidth() - VietnameseFont.WhiteBold.getWidth("Trở về")/2, this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		
		flushGraphics();
	}
	private void redraw(int i){
		clear();
		if(i==0){
			VietnameseFont.BlackBold.DrawString(g, name[i],this.getWidth()/2, this.getHeight()/2 - clickmenuImg[i].getHeight()/2 - 20, Graphics.HCENTER);
			g.drawImage(clickmenuImg[i], this.getWidth()/2 - clickmenuImg[i].getWidth()/2, this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			g.drawImage(unclickmenuImg[clickmenuName.length-1], this.getWidth()/2 - this.getWidth()/4 - unclickmenuImg[i].getWidth()/2,this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			g.drawImage(unclickmenuImg[1], this.getWidth()/2 + this.getWidth()/4 - unclickmenuImg[i].getWidth()/2,this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			
		}
		else if(i>0 && i<name.length - 1){
			VietnameseFont.BlackBold.DrawString(g, name[i],this.getWidth()/2, this.getHeight()/2 - clickmenuImg[i].getHeight()/2 - 20, Graphics.HCENTER);
			g.drawImage(clickmenuImg[i], this.getWidth()/2 - clickmenuImg[i].getWidth()/2, this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			g.drawImage(unclickmenuImg[i-1], this.getWidth()/2 - this.getWidth()/4 - unclickmenuImg[i].getWidth()/2,this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			g.drawImage(unclickmenuImg[i+1], this.getWidth()/2 + this.getWidth()/4 - unclickmenuImg[i].getWidth()/2,this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			
		}
		else if(i==name.length -1){
			VietnameseFont.BlackBold.DrawString(g, name[i],this.getWidth()/2, this.getHeight()/2 - clickmenuImg[i].getHeight()/2 - 20, Graphics.HCENTER);
			g.drawImage(clickmenuImg[i], this.getWidth()/2 - clickmenuImg[i].getWidth()/2, this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			g.drawImage(unclickmenuImg[i-1], this.getWidth()/2 - this.getWidth()/4 - unclickmenuImg[i].getWidth()/2,this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			g.drawImage(unclickmenuImg[0], this.getWidth()/2 + this.getWidth()/4 - unclickmenuImg[i].getWidth()/2,this.getHeight()/2 - clickmenuImg[i].getHeight()/2 + 20, Resources.TOP_LEFT_ANCHOR);
			
		}
		flushGraphics();
	}
	
	private void clear(){
		g.setColor(0xb8f1e2);
		g.fillRect(0, this.getHeight()/2 - 48, this.getWidth(), 96);
		flushGraphics();
	}
	
	private void menuchange(int i){
		curr += i + name.length;
        curr = curr % name.length;
	}
	public void pointerPressed(int x,int y){
		latx=x;
		laty=y;
		for(int i=this.getWidth()/2 - clickmenuImg[0].getWidth()/2;i<=this.getWidth()/2 + clickmenuImg[0].getWidth()/2;i++)
			for(int j=this.getHeight()/2 - clickmenuImg[0].getHeight()/2 + 20;j<=this.getHeight()/2 + clickmenuImg[0].getHeight()/2 + 20;j++){
				if(i==latx && j==laty){
					setnull();
					Runtime.getRuntime().gc();
					midlet.switchState(this, new startGame(midlet));
					return;
				}
			}
		for(int i=0;i<=this.getWidth()/2 - this.getWidth()/4;i++)
			for(int j=(this.getHeight()/2 - clickmenuImg[0].getHeight()/2 + 20);j<=(this.getHeight()/2 + clickmenuImg[0].getHeight()/2 + 20);j++){
				if(i==latx && j==laty){
					menuchange(-1);
					redraw(curr);
					return;
				}
			
			}
		for(int i=this.getWidth()/2 + this.getWidth()/4 - unclickmenuImg[0].getWidth()/2;i<=this.getWidth();i++)
			for(int j=this.getHeight()/2 - clickmenuImg[0].getHeight()/2 + 20;j<=this.getHeight()/2 + clickmenuImg[0].getHeight()/2 + 20;j++){
				if(i==latx && j==laty){
					menuchange(1);
					redraw(curr);
					return;
				}
			}
		for(int i=this.getWidth() - VietnameseFont.BlackBold.getWidth("Trở về");i<=this.getWidth();i++)
			for(int j=this.getHeight() - VietnameseFont.BlackBold.getHeight();j<=this.getHeight();j++){
				if(i==latx && j==laty){
					setnull();
					Runtime.getRuntime().gc();
					midlet.switchState(this, new menuTetris(midlet));
					return;
				}
			}
		for(int i=this.getWidth()/2;i<=this.getWidth()/2 + VietnameseFont.BlackBold.getWidth("Chọn");i++)
			for(int j=this.getHeight() - VietnameseFont.BlackBold.getHeight();j<=this.getHeight();j++){
				if(i==latx && j==laty){
					midlet.switchState(this, new menuSlGame(midlet));
				}
			}
	}
	public static int getkt(){
		return curr;
	}
	public void keyPressed(int keyCode) {
		
		switch (keyCode) {
		case -3:
			menuchange(-1);
			redraw(curr);
			break;
		case -4:
			menuchange(1);
			redraw(curr);
			break;
		case -5:
			setnull();
			Runtime.getRuntime().gc();
			midlet.switchState(this, new startGame(midlet));
			break;
		case -7:
			setnull();
			Runtime.getRuntime().gc();
			midlet.switchState(this, new menuTetris(midlet));
			break;
	}
	}
	private void setnull(){
		clickmenuImg=null;
		unclickmenuImg=null;
		clickmenuName=null;
		unclickmenuName=null;
		name=null;
		g=null;
	}
}